package com.example.lib5.stack;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(10);
        boolean isFull=arrayStack.isFull();
        System.out.println("是否满了"+isFull);
        boolean isEmpty=arrayStack.isEmpty();
        System.out.println("是否为空"+isEmpty);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.push(5);
        arrayStack.push(8);
        int pop = arrayStack.pop();
        System.out.println("取出的值为"+pop);
        arrayStack.list();

    }

}
class ArrayStack{

    private int maxSize;
    private int[] stack;
    private int top=-1;

    public ArrayStack(int maxSize) {
        this.maxSize=maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return top==maxSize-1;
    }

    public boolean isEmpty() {
        return top==-1;
    }

    public void push(int value) {
        //判断是否满了
        if (isFull()) {
            System.out.println("满了无法添加");
            return;
        }
        //设置对应的值
        top++;
        stack[top]=value;
    }

    public int pop() {
        //判断是否为空
        if (isEmpty()) {
            throw new RuntimeException("栈空，没有数据");
        }
        int value=stack[top];
        //取出
        stack[top]=0;
        top--;
        return value;
    }

    public void list() {
        //判断是否为空
        if (isEmpty()) {
            System.out.println("为空");
            return;
        }
        //倒遍历打印
        System.out.println("遍历结果为-----------------------");
        for (int i = top; i > -1; i--) {
            System.out.println("遍历结果为"+stack[i]);
        }
    }
}
